//
//  headImageSelectView.m
//  tabBarText
//
//  Created by tsb1119 on 2017/4/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "headImageSelectView.h"

@interface headImageSelectView()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView * tableView;
@property(strong,nonatomic)UIView * grayView;
@property(copy,nonatomic)NSArray * dataArr;
@end

@implementation headImageSelectView
-(instancetype)initWithDataArr:(NSArray *)dataArr
{
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        self.dataArr = dataArr;
        self.backgroundColor=[UIColor blackColor];
        self.alpha=0;
        UITapGestureRecognizer * panGesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
        [self addGestureRecognizer:panGesture];
        [self addSubview:self.tableView];
    }
    return self;
}
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, kUI_WINDOW_H, kUI_WINDOW_H, (_dataArr.count+1)*50+15)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [_tableView registerNib:[UINib nibWithNibName:@"headImageSelectTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"headImageSelect"];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.dataArr.count;
    }
    else
    {
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    UILabel *lab = [[UILabel alloc]init];
    [cell.contentView addSubview:lab];
    lab.font = [UIFont systemFontOfSize:15];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_offset(0);
    }];
    if (indexPath.section==0) {
        lab.text =self.dataArr[indexPath.row];
    }
    else
    {
        lab.text=@"取消".rh_localized;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0;
    }
    else
    {
        return 15;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (self.didSelected) {
            self.didSelected(indexPath.row);
        }
    }
    [self dismiss];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view =[[UIView alloc]init];
    view.backgroundColor=[UIColor lightGrayColor];
    return view;
}
-(void)show
{
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        [[UIApplication sharedApplication].keyWindow addSubview:self.tableView];
        [UIView animateWithDuration:0.2 animations:^{
            self.alpha=0.7;
            self.tableView.frame=CGRectMake(0, kUI_WINDOW_H-(self->_dataArr.count+1)*50-15, kUI_WINDOW_W, (self->_dataArr.count+1)*50+15);
        }];
}
-(void)dismiss
{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.tableView.frame=CGRectMake(0, kUI_WINDOW_H, kUI_WINDOW_W, (self->_dataArr.count+1)*50+15);
        self.alpha=0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
